﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using Lists;
using GenaralLibs;
using System.Data.SqlClient;

namespace Report
{
    public partial class FrmReport : FrmBase
    {
        public FrmReport()
        {
            InitializeComponent();
        }

        private void FrmReport_Load(object sender, EventArgs e)
        {
            tabPage1.Text = "Biểu mẫu báo cáo";

            #region cmbQuanHuyen
            cmbBieuMau.DataSource = Lists.DanhMucBieuMauBaoCao.Instance.DataView;
            cmbBieuMau.DisplayColumn = "Ten";
            cmbBieuMau.ValueColumn = "ID";
            cmbBieuMau.AddVisibleColumn("Ten", "Tên", 400);
            #endregion

            UpdatePhieuBPhuThuocPhieuA();
        }

        private DevExpress.XtraEditors.XtraUserControl ChooseUcControl(string bieuMauChoosed)
        {
            DevExpress.XtraEditors.XtraUserControl uCBaoCao = null;
            switch (bieuMauChoosed.Trim().ToUpper())
            {
                case "UCA1":
                    uCBaoCao = new Report.PhieuA.UcPhieuA1();
                    break;
                case "UCA2":
                    uCBaoCao = new Report.PhieuA.UcA2();
                    break;
                case "UCA3":
                    uCBaoCao = new Report.PhieuA.UcA3();
                    break;
                case "UCA4":
                    uCBaoCao = new Report.PhieuA.UcA4();
                    break;
                case "UCA5":
                    uCBaoCao = new Report.PhieuA.UcA5();
                    break;
                case "UCA6":
                    uCBaoCao = new Report.PhieuA.UcA6();
                    break;
                case "UCA7":
                    uCBaoCao = new Report.PhieuA.UcA7();
                    break;
                case "UCA8":
                    uCBaoCao = new Report.PhieuA.UcA8();
                    break;
                case "UCA9":
                    uCBaoCao = new Report.PhieuA.UcA9();
                    break;
                case "UCA10":
                    uCBaoCao = new Report.PhieuA.UcA10();
                    break;

                /////////////////
                case "UCA11":
                    uCBaoCao = new Report.PhieuA.UcA11();
                    break;
                case "UCA12":
                    uCBaoCao = new Report.PhieuA.UcA12();
                    break;
                case "UCA13":
                    uCBaoCao = new Report.PhieuA.UcA13();
                    break;
                case "UCA14":
                    uCBaoCao = new Report.PhieuA.UcA14();
                    break;
                case "UCA15":
                    uCBaoCao = new Report.PhieuA.UcA15();
                    break;
                case "UCA16":
                    uCBaoCao = new Report.PhieuA.UcA16();
                    break;
                case "UCA17":
                    uCBaoCao = new Report.PhieuA.UcA17();
                    break;
                case "UCA18":
                    uCBaoCao = new Report.PhieuA.UcA18();
                    break;
                case "UCA19":
                    uCBaoCao = new Report.PhieuA.UcA19();
                    break;
                case "UCA20":
                    uCBaoCao = new Report.PhieuA.UcA20();
                    break;

                /////////////////
                case "UCA21":
                    uCBaoCao = new Report.PhieuA.UcA21();
                    break;
                case "UCA22":
                    uCBaoCao = new Report.PhieuA.UcA22();
                    break;
                case "UCA23":
                    uCBaoCao = new Report.PhieuA.UcA23();
                    break;
                case "UCA24":
                    uCBaoCao = new Report.PhieuA.UcA24();
                    break;
                case "UCA25":
                    uCBaoCao = new Report.PhieuA.UcA25();
                    break;
                case "UCA26":
                    uCBaoCao = new Report.PhieuA.UcA26();
                    break;
                case "UCA27":
                    uCBaoCao = new Report.PhieuA.UcA27();
                    break;
                case "UCA28":
                    uCBaoCao = new Report.PhieuA.UcA28();
                    break;
                case "UCA29":
                    uCBaoCao = new Report.PhieuA.UcA29();
                    break;
                case "UCA30":
                    uCBaoCao = new Report.PhieuA.UcA30();
                    break;
                /////////////////
                case "UCA31":
                    uCBaoCao = new Report.PhieuA.UcA31();
                    break;
                case "UCA32":
                    uCBaoCao = new Report.PhieuA.UcA32();
                    break;
                case "UCA33":
                    uCBaoCao = new Report.PhieuA.UcA33();
                    break;
                case "UCA34":
                    uCBaoCao = new Report.PhieuA.UcA34();
                    break;
                case "UCA35":
                    uCBaoCao = new Report.PhieuA.UcA35();
                    break;
                case "UCA36":
                    uCBaoCao = new Report.PhieuA.UcA36();
                    break;
                case "UCA37":
                    uCBaoCao = new Report.PhieuA.UcA37();
                    break;
                case "UCA38":
                    uCBaoCao = new Report.PhieuA.UcA38();
                    break;
                case "UCA39":
                    uCBaoCao = new Report.PhieuA.UcA39();
                    break;
                case "UCA40":
                    uCBaoCao = new Report.PhieuA.UcA40();
                    break;

                /////////////////
                case "UCA41":
                    uCBaoCao = new Report.PhieuA.UcA41();
                    break;
                case "UCA42":
                    uCBaoCao = new Report.PhieuA.UcA42();
                    break;
                case "UCA43":
                    uCBaoCao = new Report.PhieuA.UcA43();
                    break;
                case "UCA44":
                    uCBaoCao = new Report.PhieuA.UcA44();
                    break;
                case "UCA45":
                    uCBaoCao = new Report.PhieuA.UcA45();
                    break;
                case "UCA46":
                    uCBaoCao = new Report.PhieuA.UcA46();
                    break;
                case "UCA47":
                    uCBaoCao = new Report.PhieuA.UcA47();
                    break;
                case "UCA48":
                    uCBaoCao = new Report.PhieuA.UcA48();
                    break;
                case "UCA49":
                    uCBaoCao = new Report.PhieuA.UcA49();
                    break;
                case "UCA50":
                    uCBaoCao = new Report.PhieuA.UcA50();
                    break;

                /////////////////
                case "UCA51":
                    uCBaoCao = new Report.PhieuA.UcA51();
                    break;
                case "UCA52":
                    uCBaoCao = new Report.PhieuA.UcA52();
                    break;
                case "UCA53":
                    uCBaoCao = new Report.PhieuA.UcA53();
                    break;
                case "UCA54":
                    uCBaoCao = new Report.PhieuA.UcA54();
                    break;
                case "UCB55":
                    uCBaoCao = new Report.PhieuB.UcB55();
                    break;
                case "UCB56":
                    uCBaoCao = new Report.PhieuB.UcB56();
                    break;
                case "UCB57":
                    uCBaoCao = new Report.PhieuB.UcB57();
                    break;
                case "UCB58":
                    uCBaoCao = new Report.PhieuB.UcB58();
                    break;
                case "UCB59":
                    uCBaoCao = new Report.PhieuB.UcB59();
                    break;
                case "UCB60":
                    uCBaoCao = new Report.PhieuB.UcB60();
                    break;

                /////////////////
                case "UCB61":
                    uCBaoCao = new Report.PhieuB.UcB61();
                    break;
                case "UCB62":
                    uCBaoCao = new Report.PhieuB.UcB62();
                    break;
                case "UCB63":
                    uCBaoCao = new Report.PhieuB.UcB63();
                    break;
                case "UCB64":
                    uCBaoCao = new Report.PhieuB.UcB64();
                    break;
                case "UCB65":
                    uCBaoCao = new Report.PhieuB.UcB65();
                    break;
                case "UCB66":
                    uCBaoCao = new Report.PhieuB.UcB66();
                    break;
                case "UCB67":
                    uCBaoCao = new Report.PhieuB.UcB67();
                    break;
                case "UCB68":
                    uCBaoCao = new Report.PhieuB.UcB68();
                    break;
                case "UCB69":
                    uCBaoCao = new Report.PhieuB.UcB69();
                    break;
                case "UCB70":
                    uCBaoCao = new Report.PhieuB.UcB70();
                    break;

                /////////////////
                case "UCB71":
                    uCBaoCao = new Report.PhieuB.UcB71();
                    break;
                case "UCC72":
                    uCBaoCao = new Report.PhieuC.UcC72();
                    break;
                case "UCC73":
                    uCBaoCao = new Report.PhieuC.UcC73();
                    break;
                case "UCC74":
                    uCBaoCao = new Report.PhieuC.UcC74();
                    break;
                case "UCC75":
                    uCBaoCao = new Report.PhieuC.UcC75();
                    break;
                case "UCC76":
                    uCBaoCao = new Report.PhieuC.UcC76();
                    break;
                case "UCC77":
                    uCBaoCao = new Report.PhieuC.UcC77();
                    break;
                case "UCC78":
                    uCBaoCao = new Report.PhieuC.UcC78();
                    break;
                case "UCC79":
                    uCBaoCao = new Report.PhieuC.UcC79();
                    break;
                case "UCC80":
                    uCBaoCao = new Report.PhieuC.UcC80();
                    break;

                /////////////////
                case "UCC81":
                    uCBaoCao = new Report.PhieuC.UcC81();
                    break;
                case "UCC82":
                    uCBaoCao = new Report.PhieuC.UcC82();
                    break;
                case "UCC83":
                    uCBaoCao = new Report.PhieuC.UcC83();
                    break;
                case "UCC84":
                    uCBaoCao = new Report.PhieuC.UcC84();
                    break;
                case "UCC85":
                    uCBaoCao = new Report.PhieuC.UcC85();
                    break;
                case "UCC86":
                    uCBaoCao = new Report.PhieuC.UcC86();
                    break;
                case "UCC87":
                    uCBaoCao = new Report.PhieuC.UcC87();
                    break;
                case "UCC88":
                    uCBaoCao = new Report.PhieuC.UcC88();
                    break;
                case "UCC89":
                    uCBaoCao = new Report.PhieuC.UcC89();
                    break;
                case "UCC90":
                    uCBaoCao = new Report.PhieuC.UcC90();
                    break;

                /////////////////
                case "UCD91":
                    uCBaoCao = new Report.PhieuD.UcD91();
                    break;
                case "UCD92":
                    uCBaoCao = new Report.PhieuD.UcD92();
                    break;
                case "UCD93":
                    uCBaoCao = new Report.PhieuD.UcD93();
                    break;
                case "UCD94":
                    uCBaoCao = new Report.PhieuD.UcD94();
                    break;
                case "UCD95":
                    uCBaoCao = new Report.PhieuD.UcD95();
                    break;
                case "UCD96":
                    uCBaoCao = new Report.PhieuD.UcD96();
                    break;
                case "UCD97":
                    uCBaoCao = new Report.PhieuD.UcD97();
                    break;
                case "UCD98":
                    uCBaoCao = new Report.PhieuD.UcD98();
                    break;
                case "UCD99":
                    uCBaoCao = new Report.PhieuD.UcD99();
                    break;
                case "UCD100":
                    uCBaoCao = new Report.PhieuD.UcD100();
                    break;

                /////////////////
                case "UCD101":
                    uCBaoCao = new Report.PhieuD.UcD101();
                    break;
                case "UCD102":
                    uCBaoCao = new Report.PhieuD.UcD102();
                    break;
                case "UCD103":
                    uCBaoCao = new Report.PhieuD.UcD103();
                    break;
                case "UCD104":
                    uCBaoCao = new Report.PhieuD.UcD104();
                    break;
                case "UCD105":
                    uCBaoCao = new Report.PhieuD.UcD105();
                    break;
                case "UCD106":
                    uCBaoCao = new Report.PhieuD.UcD106();
                    break;
                case "UCD107":
                    uCBaoCao = new Report.PhieuD.UcD107();
                    break;
                case "UCD108":
                    uCBaoCao = new Report.PhieuD.UcD108();
                    break;
                case "UCD109":
                    uCBaoCao = new Report.PhieuD.UcD109();
                    break;
                case "UCD110":
                    uCBaoCao = new Report.PhieuD.UcD110();
                    break;
                case "UCD111":
                    uCBaoCao = new Report.PhieuD.UcD111();
                    break;
                case "UCD112":
                    uCBaoCao = new Report.PhieuD.UcD112();
                    break;
                case "UCD113":
                    uCBaoCao = new Report.PhieuD.UcD113();
                    break;
                default:
                    break;
            }
            return uCBaoCao;
        }

        private void cmbBieuMau_EditValueChanged(object sender, EventArgs e)
        {
            if(!(cmbBieuMau.EditValue is Guid) || (Guid)cmbBieuMau.EditValue == Guid.Empty) return;

            DevExpress.XtraEditors.XtraUserControl uCBaoCao = null;
            Lists.BieuMauBaoCao bieuMauChoosed = Lists.DanhMucBieuMauBaoCao.Instance.GetByID((Guid)cmbBieuMau.EditValue);

            uCBaoCao = ChooseUcControl(bieuMauChoosed.Ma.Trim().ToString());
           
            tabPage1.Controls.Clear();
            tabPage1.Controls.Add(uCBaoCao);
            tabPage1.Text = bieuMauChoosed.Ten;
        }

        void UpdatePhieuBPhuThuocPhieuA()
        {
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            SqlCommand cmd = con.CreateCommand();
            try
            {
                con.Open();
                cmd.CommandText = "UPDATE Phieu SET GhiChu = 'A' + SUBSTRING(MaPhieu, 2,3) WHERE LoaiPhieu = 'PhieuB'";
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            foreach (DevExpress.XtraTab.XtraTabPage tp in tabControl.TabPages)
            {
                foreach (Control ct in tp.Controls)
                {
                    if (ct is  DevExpress.XtraEditors.XtraUserControl)//DevExpress.XtraGrid.GridControl)
                    {
                        foreach (Control grid in ct.Controls)
                        {
                            if (grid is DevExpress.XtraGrid.GridControl)
                            {
                                (grid as DevExpress.XtraGrid.GridControl).ShowPrintPreview();
                            }
                        }
                    }
                }
            }
        }

        private DataSet LoadAllUcForExportExcel()
        {
            DataSet ds = new DataSet();
            DevExpress.XtraEditors.XtraUserControl uCBaoCao = null;

            foreach (DataRow bieuMau in Lists.DanhMucBieuMauBaoCao.GetAllAndSort().Rows)
            {
                uCBaoCao = ChooseUcControl(bieuMau["Ma"].ToString().ToUpper());
                tabPage1.Controls.Add(uCBaoCao);
                //Đưa vào dataset
                foreach (Control grid in uCBaoCao.Controls)
                {
                    if (grid is DevExpress.XtraGrid.GridControl)
                    {
                        DataTable dt = ((grid as DevExpress.XtraGrid.GridControl).DataSource as DataTable);
                        dt.TableName = "Bieu" + ds.Tables.Count;
                        ds.Tables.Add(dt);
                    }
                }
            }

            return ds;
        }

        private void btnExportAllExcel_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = new DataSet();
                ds = LoadAllUcForExportExcel();
                ExportToExcel.ExportDataToExcel(ds, "Tổng hợp báo cáo BHXH 2012");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message);
            }
        }
    }
}
